package 栈和队列;

import java.util.Stack;

public class LC_150逆波兰表达式求值 {
    public static void main(String[] args){
        String[] tokens={"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        System.out.println(evalRPN(tokens));
    }
    public static int evalRPN(String[] tokens) {
        Stack<Integer> st=new Stack<>();
        for(int i=0;i<tokens.length;i++){
            if(tokens[i].equals("+"))
                st.push(st.pop()+st.pop());
            else if(tokens[i].equals("/")){
                int nums1=st.pop();
                int nums2=st.pop();
                st.push(nums2-nums1);
            }
            else if(tokens[i].equals("*")){
                int nums1=st.pop();
                int nums2=st.pop();
                st.push(nums1*nums2);
            }
            else if(tokens[i].equals("/")){
                int nums1=st.pop();
                int nums2=st.pop();
                st.push(nums2/nums1);
            }
            else
                st.push(Integer.valueOf(tokens[i]));
        }
        return st.pop();
    }
}
